<?php
/**
 * Created by PhpStorm.
 * User: yzg
 * Date: 2016/1/19
 * Time: 14:12
 */

class Weixin_vote_module extends CI_Module
{
    public $weixin_model;
    public $vote_model;
    public $setting;

    public function __construct()
    {
        parent::__construct();
        $this->load->model('weixin.vote_model');

        if(!ADMINID){
            redirect(site_url('admin/index/login'));
        }
    }

    public function index()
    {
        $this->data['list'] = $this->vote_model->get_rows();
        $this->load->view('admin/vote_list',$this->data);
    }

    public function votelog(){
        $id = intval($_GET['id']);
        $this->load->view('admin/votelog_list',$this->data);
    }

    public function add()
    {
        if(strtolower($_SERVER['REQUEST_METHOD']) == 'post')
        {
            $_POST['Form']['tuanxing'] = serialize(trim($_POST['Form']['tuanxing']));
            if($form = $_POST['Form'])
            {
                if($id = intval($_POST['id'])){
                    $form['upts'] = time();
                    if($this->vote_model->edit_row($form, array('id' => $id))){
                        $this->success('编辑成功',site_url('weixin/vote'),true);
                    }

                }else{

                    unset($form['id']);
                    $form['crts'] = $form['upts'] = time();
                    if($this->vote_model->add_row($form)){
                        $this->success('添加成功',site_url('weixin/vote'),true);
                    }
                }
            }

            $this->error('添加失败！',site_url('weixin/vote/add'),true);
        }
        $this->load->view('admin/vote_add',$this->data);
    }

    public function edit(){
        $id= intval($_GET['id']);
        $this->data['data'] = $this->vote_model->get_row(array('id' => $id));
        $this->data['data']['tuanxing'] = unserialize($this->data['data']['tuanxing']);

        $this->add();
    }

    public function vote_delete(){
        $id = intval($_REQUEST['id']);
        if($r = $this->vote_model->remove_all($id)){
            $this->success('删除成功',site_url('weixin/vote'),true);
        }
        $this->error('删除失败！',site_url('weixin/vote'),true);
    }

}